package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.definterface.OnChooseMeListener;
import com.xmb.wechat.definterface.SimpleTextWatcher;
import com.xmb.wechat.bean.PYQCommentsBean;
import com.xmb.wechat.widget.TitleLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/9 15:31
 */
public class WechatPYQAddCommentsActivity extends BaseActivity {

    @BindView(R2.id.title_layout)
    TitleLayout  mTitleLayout;
    @BindView(R2.id.btn_comment)
    TextView     mBtnComment;
    @BindView(R2.id.btn_reply)
    TextView     mBtnReply;
    @BindView(R2.id.tv_sender)
    TextView     mTvSender;
    @BindView(R2.id.tv_reply)
    TextView     mTvReply;
    @BindView(R2.id.et)
    EditText     mEt;
    @BindView(R2.id.ll_reply)
    LinearLayout mLlReply;
    @BindView(R2.id.btn_preview)
    Button       mBtnPreview;

    private boolean           isAdd;
    private WechatContactBean sender, replyer;
    private PYQCommentsBean mEntity, mDeliver;

    public WechatPYQAddCommentsActivity() {
        super(R.layout.activity_wechat_pyq_add_comments);
    }

    public static void start4Result(Activity ctx, PYQCommentsBean entity, int requestCode) {
        Intent intent = new Intent(ctx, WechatPYQAddCommentsActivity.class);
        intent.putExtra("data", entity);
        ctx.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doSave();
            }
        });
        //数据
        mEntity = (PYQCommentsBean) getIntent().getSerializableExtra("data");
        if (mEntity == null) {//新增
            isAdd = true;
            mEntity = new PYQCommentsBean();
            setCommentTypeState(true);
        } else {//编辑
            mDeliver = new PYQCommentsBean(mEntity.getId(), mEntity.getType(), mEntity.getCommenter(), mEntity.getCommenteder(), mEntity.getCommentValue());
            mBtnPreview.setText("修改");
            mTitleLayout.setTitle("编辑评论");
            mTitleLayout.setTitleRight("修改");
            //showView
            setCommentTypeState(mEntity.getType() == 0);
            showSender(mEntity.getCommenter());
            showReplyer(mEntity.getCommenteder());
            mEt.setText(TextUtils.isEmpty(mEntity.getCommentValue()) ? "" : mEntity.getCommentValue());
        }
        mEt.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mEntity.setCommentValue(value);
            }
        });
    }

    @OnClick({R2.id.btn_comment, R2.id.btn_reply, R2.id.ll_sender, R2.id.ll_reply, R2.id.btn_preview})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_comment) {
            setCommentTypeState(true);
        } else if (id == R.id.btn_reply) {
            setCommentTypeState(false);
        } else if (id == R.id.ll_sender) {
            showRoleChooseSender(this,true, false, new OnChooseMeListener() {
                @Override
                public void onMeChoose(WechatContactBean mineContactBean) {
                    if (replyer != null && replyer.getId() == mineContactBean.getId()) {
                        ToastUtils.showShort("评论对象与回复对象不能相同");
                        return;
                    }
                    sender = mineContactBean;
                    showSender(mineContactBean.getName());
                }
            });
        } else if (id == R.id.ll_reply) {
            showRoleChooseReceiver(this,true, false, new OnChooseMeListener() {
                @Override
                public void onMeChoose(WechatContactBean mineContactBean) {
                    if (sender != null && sender.getId() == mineContactBean.getId()) {
                        ToastUtils.showShort("回复对象与评论对象不能相同");
                        return;
                    }
                    replyer = mineContactBean;
                    showReplyer(mineContactBean.getName());
                }
            });
        } else if (id == R.id.btn_preview) {
            doSave();
        }
    }

    private void showSender(String name) {
        mTvSender.setText(TextUtils.isEmpty(name) ? "" : name);
        mEntity.setCommenter(name);
    }

    private void showReplyer(String name) {
        mTvReply.setText(TextUtils.isEmpty(name) ? "" : name);
        mEntity.setCommenteder(name);
    }

    private void setCommentTypeState(boolean isComment) {
        mBtnComment.setSelected(isComment);
        mBtnReply.setSelected(!isComment);
        mLlReply.setVisibility(isComment ? View.GONE : View.VISIBLE);
        if (isComment) {
            replyer = null;
            mTvReply.setText("请选择");
        }
        mEntity.setType(isComment ? 0 : 1);
    }

    private void doSave() {
        String value = mEt.getText().toString().trim();
        if (TextUtils.isEmpty(value)) {
            ToastUtils.showShort("请输入评论内容");
            return;
        }
        if (TextUtils.isEmpty(mEntity.getCommenter())) {
            ToastUtils.showShort("请选择评论好友");
            return;
        }
        if (mEntity.getType() == 1 && TextUtils.isEmpty(mEntity.getCommenteder())) {
            ToastUtils.showShort("请选择回复对象");
            return;
        }
        mEntity.setCommentValue(value);
        Intent intent = new Intent();
        intent.putExtra("data", mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE_OTHER_SEND || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_SEND) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (result == null)
                return;
            if (replyer != null && replyer.getId() == result.getId()) {
                ToastUtils.showShort("评论对象与回复对象不能相同");
                return;
            }
            sender = result;
            showSender(result.getName());
        } else if (requestCode == REQUEST_CODE_CHOOSE_OTHER_RECEIVE || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_RECEIVE) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (result == null)
                return;
            if (sender != null && sender.getId() == result.getId()) {
                ToastUtils.showShort("回复对象与评论对象不能相同");
                return;
            }
            replyer = result;
            showReplyer(result.getName());
        }
    }

    @Override
    protected boolean isNeedShowLeaveHint() {
        if (isAdd)
            return !mEntity.equals(new PYQCommentsBean());
        else
            return !mEntity.equals(mDeliver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KeyboardUtils.hideSoftInput(WechatPYQAddCommentsActivity.this);
    }
}
